package com.itheima.service.impl;

import com.itheima.dao.RoleDao;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {
    @Override
    public List<Role> findAll() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        List<Role> list = mapper.findAll();
        sqlSession.close();
        return list;
    }

    @Override
    public void add(Role role) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        String id = UUID.randomUUID().toString();
        role.setRoleId(id);
        role.setCreateTime(new Date());
        mapper.add(role);
        sqlSession.close();
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        mapper.delete(id);
        sqlSession.close();
    }

    @Override
    public void update(Role role) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        RoleDao mapper = sqlSession.getMapper(RoleDao.class);
        role.setUpdateTime(new Date());
        mapper.update(role);
        sqlSession.close();
    }
}
